<template>
  <div class="login_container">
    <div class="login_box">
      <!-- 头像区域 -->
      <div class="avatar_box">
        <img src="../../assets/logo2.png" alt="">
      </div>
      <!-- 登录表单区域 -->
      <el-form ref="loginFormRef" :model="loginForm" :rules="loginFormRules" label-width="0px" class="login_form">
        <!-- 登录类型 -->
        <div class="el-radio-group">
          <el-radio-group v-model="userType" >
            <el-radio-button :label="1">客户</el-radio-button>
            <el-radio-button :label="2">律师</el-radio-button>
          </el-radio-group>
        </div>
        <!-- 用户名 -->
        <el-form-item prop="username">
          <el-input v-model="loginForm.username" prefix-icon="iconfont icon-user" placeholder="请输入用户名"></el-input>
        </el-form-item>
        <!-- 密码 -->
        <el-form-item prop="password">
          <el-input v-model="loginForm.password" prefix-icon="iconfont icon-3702mima" type="password" placeholder="请输入密码"></el-input>
        </el-form-item>
        <!--          昵称-->
        <el-form-item prop="nickname">
          <el-input v-model="loginForm.nickname" prefix-icon="iconfont icon-3702mima" placeholder="请输入昵称"></el-input>
        </el-form-item>
        <!--          邮箱-->
        <el-form-item prop="mail">
          <el-input v-model="loginForm.mail" prefix-icon="iconfont icon-3702mima" placeholder="请输入邮箱"></el-input>
        </el-form-item>
        <!-- 按钮区域 -->
        <el-form-item class="btns">
          <el-button type="primary" @click="register">注册</el-button>
          <el-button type="info" @click="resetLoginForm">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
import axios from "axios";
export default {
  name: "Register",
  data() {
    return {
      // 用户类型
      userType: 1,
      loginForm: {
        username: '',
        password: '',
        nickname: '',
        mail: '',
        type: '民事类',
      },
      // 这是表单的验证规则对象
      loginFormRules: {
        username: [
          { required: true, message: '请输入用户名称', trigger: 'blur' },
          { min: 1, max: 10, message: '长度在 1 到 10 个字符', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 1, max: 15, message: '长度在 1 到 15 个字符', trigger: 'blur' }
        ]
      }
    }
  },
  computed: {
    newUser() {
      return {
        id: '',
        username: this.loginForm.username,
        password: this.loginForm.password,
        nickname: this.loginForm.nickname,
        type: this.loginForm.type
      }
    },
    newLawyer() {
      return {
        id: '',
        lawyer_username: this.loginForm.username,
        lawyer_password: this.loginForm.password,
        name: this.loginForm.nickname,
        age: 23,
        gender: "男",
        worktime: 8,
        cases: 55,
        descrip: "暂无",
        free: true,
        casetype: this.loginForm.type,
      }
    }
  },
  methods: {
    // 点击重置按钮，重置登录表单
    resetLoginForm() {
      // console.log(this.userType);
      this.$refs.loginFormRef.resetFields()
    },
    register() {
      switch (this.userType) {
        case 1 :
          console.log(this.userType + "is 客户");
          console.log(JSON.stringify(this.newUser))
          axios.post("http://localhost:8080/user/addUser", {
              user: this.newUser
          }).then(function (response) {
            console.log(response);
          })
              .catch(function (error) {
                console.log(error);
              });
          break;
        case 2 :
          console.log(this.userType + "is 律师");
          axios.post("http://localhost:8080/lawyer/addLawyer", {
            user: this.newLawyer
          }).then(function (response) {
            console.log(response);
          })
              .catch(function (error) {
                console.log(error);
              });
          break;
        default:
          console.log("ERROR");
      }
      window.location = "home.html"
    },
    loginError() {
      this.$message({
        showClose: true,
        message: '用户名或密码错误',
        type: 'error'
      });
    }
  }
}
</script>

<style lang="less" scoped>
body {
  background-color: #2c3e50;
}

.login_container {
  background-color: #2c3e50;
  //height: 100%;
}

.login_box {
  width: 450px;
  height: 300px;
  background-color: #fff;
  border-radius: 3px;
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);

  .avatar_box {
    height: 130px;
    width: 130px;
    border: 1px solid #eee;
    border-radius: 50%;
    padding: 10px;
    box-shadow: 0 0 10px #409EFF;
    position: absolute;
    left: 50%;
    top: -20%;
    transform: translate(-50%, -50%);
    background-color: #fff;
    img {
      width: 100%;
      height: 100%;
      border-radius: 50%;
      background-color: #eee;
    }
  }
}

.login_form {
  margin-bottom: -100px;
  position: absolute;
  bottom: 0;
  width: 100%;
  padding: 0 20px;
  box-sizing: border-box;
  text-align: center;
  background: no-repeat 50% 0;
  background-size: 400px 300px;
  background-image: url("https://ss0.bdstatic.com/70cFvHSh_Q1YnxGkpoWK1HF6hhy/it/u=3665364811,86340695&fm=26&gp=0.jpg");
}

.btns {
  display: flex;
  justify-content: flex-end;
}

.el-radio-group {
  margin-bottom: 3px;
}

</style>